//
//  SCStoreController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/6/21.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCStoreController.h"
#import "ZJScrollPageView.h"
#import "SCStoreGoodsListController.h"

@interface SCStoreController ()<ZJScrollPageViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *searchTF;

@property (strong, nonatomic) NSArray *segmentArray;

@end

@implementation SCStoreController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.lineView.hidden = NO;
    self.searchTF.delegate = self;
    self.searchTF.returnKeyType = UIReturnKeySearch;
    
    //请求数据
    [self requestSegmentData];
}
- (void)requestSegmentData{
    
    NSDictionary *dict = @{@"store_id":self.store_id,
                           @"size":@(10),
                           @"pagenow":@(1)};
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=StoreApp&a=Store_list"];
    
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        NSLog(@"===%@",result);
        self.segmentArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"inside_catgory"]];
        if (self.segmentArray.count > 0) {
            NSMutableArray *titles = [NSMutableArray array];
            for (SCModel *model in self.segmentArray) {
                if (model.cat_name != nil) {
                    [titles addObject:model.cat_name];
                }
            }
            if (titles.count > 0) {
                [self setupSegmentView:[titles copy]];
            }
        }
    }];
}
- (void)setupSegmentView:(NSArray *)titles{
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    style.showLine = YES;//显示遮盖
    style.scrollLineColor = RGB(231, 157, 58);
    style.titleFont = SCFont(14);
    style.segmentHeight = 35;
    // 当标题(和图片)宽度总和小于ZJScrollPageView的宽度的时候, 标题会自适应宽度
    style.autoAdjustTitlesWidth = YES;
    style.contentViewBounces = NO;
    style.gradualChangeTitleColor = YES;// 颜色渐变
    style.normalTitleColor = RGB(176, 176, 176);
    style.selectedTitleColor = RGB(231, 157, 58);
    // 初始化
    ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 44, kScreenW, kScreenH - kTopHeight - 44) segmentStyle:style titles:titles parentViewController:self delegate:self];
    [self.view addSubview:scrollPageView];
}
#pragma mark - 代理
- (NSInteger)numberOfChildViewControllers {
    return self.segmentArray.count;
}
- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index{
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = self.childVcs[index];
    }
    return childVc;
}
- (NSArray *)childVcs{
    
    NSMutableArray *childs = [NSMutableArray array];
    for (SCModel *model in self.segmentArray) {
        SCStoreGoodsListController *vc = [[SCStoreGoodsListController alloc]init];
        vc.store_id = self.store_id;
        vc.cat_id = model.cat_id;
        [childs addObject:vc];
    }
    return [childs copy];
}
#pragma mark - 搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入搜索内容"];
        return NO;
    }
    [textField resignFirstResponder];
    
    SCStoreGoodsListController *vc = [[SCStoreGoodsListController alloc]init];
    vc.store_id = self.store_id;
    vc.goods_name = textField.text;
    vc.isSearch = YES;
    vc.title = @"商品列表";
    [self.navigationController pushViewController:vc animated:YES];
    return YES;
}

@end
